$(document).ready(function(){
    var $content=$('#content');
    $content.find('input[type=number].integer').wl_Number();
    $content.find('input[type=number].decimal').wl_Number({
        decimals:2,
        step:0.5
    });
    $content.find('input.date, div.date').wl_Date();
    $content.find('input.time').wl_Time();
    $content.find('textarea[data-autogrow]').elastic();
    $content.find('textarea.html').wl_Editor();
    $content.find('input[data-regex]').wl_Valid();
    $content.find('input[type=email]').wl_Mail();
    $content.find('input[type=url]').wl_URL();
    if(($('input[type=file]')).html() != null) $content.find('input[type=file]').wl_File({
        url:url_upload,
        paramName: paramName,
        autoUpload:autoUpload,
        maxNumberOfFiles: maxNumberOfFiles,
        multiple: multiple,
        allowedExtensions:allowedExtensions,
        formData:{},
        text:{
            ready:'pronto',
            cancel:'cancelar',
            remove:'remover',
            uploading:'carregando...',
            done:'concluído',
            choose_file: 'arquivo',
            start:'começar a carregar',
            add_files:'adicionar arquivos',
            cancel_all:'cancelar arquivo',
            remove_all:'remover todos'
        },
        onFinish:function(e,data){
            var result=$.parseJSON(data.result);
            $('input[type=file]').closest('form').prepend('<input type="hidden" name="images['+result.id+']" value="'+result.id+'">');
            $.msg(result.msg,{header:result.header,live:10000});
        },
        onFileError:function(error,fileobj){
            $.msg(language.form.file+fileobj.name,{
                header:error.msg+' ('+error.code+')'
            });
        }
    });
    $content.find('input[type=password]').wl_Password();
    $content.find('input.color').wl_Color();
    $content.find('div.slider').wl_Slider();
    $content.find('select[multiple]').wl_Multiselect();
    $content.find('form').wl_Form();
    $content.find('div.alert').wl_Alert();
    $("select, input[type=file]").not('select[multiple]').uniform();
    if(($('input:checkbox, input:radio')).html() != null) $('input:checkbox, input:radio').checkbox();
    $content.find('input[title]').tipsy({
        gravity:function(){
            return($(this).data('tooltip-gravity')||config.tooltip.gravity);
        },
        fade:config.tooltip.fade,
        opacity:config.tooltip.opacity,
        color:config.tooltip.color,
        offset:config.tooltip.offset
    });
    
    var $searchform=$('#searchform'),$searchfield=$('#search'),livesearch=true;
    
    $searchfield.bind({
        'focus.wl':function(){
            $searchfield.select().parent().animate({
                width:'150px'
            },100);
        },
        'blur.wl':function(){
            $searchfield.parent().animate({
                width:'90px'
            },100);
            if(livesearch)$searchboxresult.fadeOut();
        }
    });
    if(livesearch){
        var $searchboxresult=$('#searchboxresult'),searchdelay=800,searchminimum=3,searchtimeout,searchterm,resulttitle;
        if(!$searchboxresult.length)$searchboxresult=$('<div id="searchboxresult"></div>').insertAfter('#searchbox');
        $searchfield.bind({
            'keyup.wl':function(event){
                if(searchterm==$searchfield.val())return false;
                searchterm=$searchfield.val();
                clearTimeout(searchtimeout);
                if(searchterm.length<searchminimum){
                    $searchboxresult.fadeOut();
                    $searchfield.removeClass('load');
                    return false;
                }
                searchtimeout=setTimeout(function(){
                    $searchfield.addClass('load');
                    $.post("search.php",{
                        term:searchterm
                    },function(data){
                        $searchfield.removeClass('load');
                        if(searchterm.length<searchminimum){
                            $searchboxresult.fadeOut();
                            return false;
                        }
                        var count=data.length,html='';
                        if(count){
                            for(var i=0;i<count;i++){
                                resulttitle='';
                                if(data[i].text.length>105){
                                    resulttitle='title="'+data[i].text+'"';
                                    data[i].text=$.trim(data[i].text.substr(0,100))+'…';
                                }
                                html+='<li><a href="'+data[i].href+'" '+resulttitle+'>';
                                if(data[i].img)html+='<img src="'+data[i].img+'" width="50">';
                                html+=''+data[i].text+'</a></li>';
                            }
                        }else{
                            html+='<li><a class="noresult">'+language.form.livesearch+'<br>"'+searchterm+'"</a></li>';
                        }
                        $searchboxresult.html(html).fadeIn();
                    },"json");
                },searchdelay);
            }
        });
    }
    
    $searchform.bind('submit.wl',function(){
        var query=$searchfield.val();
    });
    
    $("[placeholder]").bind('focus.placeholder',function(){
        var el=$(this);
        if(el.val()==el.attr("placeholder")&&!el.data('uservalue')){
            el.val("");
            el.removeClass("placeholder");
        }
    }).bind('blur.placeholder',function(){
        var el=$(this);
        if(el.val()==""||el.val()==el.attr("placeholder")&&!el.data('uservalue')){
            el.addClass("placeholder");
            el.val(el.attr("placeholder"));
            el.data("uservalue",false);
        }else{}
    }).bind('keyup.placeholder',function(){
        var el=$(this);
        if(el.val()==""){
            el.data("uservalue",false);
        }else{
            el.data("uservalue",true);
        }
    }).trigger('blur.placeholder');
});